package com.soup.lab.spring.web.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonView;
import com.soup.lab.spring.web.pojo.ExamplePojo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

@RestController
public class DemoController {

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

    @ModelAttribute
    public Cloth getCloth() {
        return new Cloth("black", 180);
    }

    @RequestMapping("/c")
    @JsonView(Cloth.SizeOnly.class)
    public Cloth getCloth(Cloth cloth) {
        System.out.println(cloth);
        return cloth;
    }

    @RequestMapping("/man")
    public Man getMan(Man man) {
        System.out.println(man);
        return man;
    }

    @RequestMapping("/example")
    public ExamplePojo getExample() {
        return new ExamplePojo(1, "1", new Date());
    }

    @RequestMapping("/examples")
    public Collection<ExamplePojo> getExamples() {
        return Arrays.asList(new ExamplePojo(1, "1", new Date()), new ExamplePojo(2, "2", new Date()));
    }

    @RequestMapping("/examples2")
    public Collection<Collection<ExamplePojo>> examples2() {
        return Arrays.asList(Arrays.asList(new ExamplePojo(1, "1", new Date()), new ExamplePojo(2, "2", new Date())));
    }

    @RequestMapping("/exampleArr")
    public ExamplePojo[] exampleArr() {
        return new ExamplePojo[]{new ExamplePojo(1, "1", new Date()), new ExamplePojo(2, "2", new Date())};
    }

    @RequestMapping("/exampleArr2")
    public ExamplePojo[][] exampleArr2() {
        return new ExamplePojo[][]{new ExamplePojo[]{new ExamplePojo(1, "1", new Date()), new ExamplePojo(2, "2", new Date())}};
    }

    public static class Man {
        @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
        private Date birthday;

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }


    public static class Cloth {

        interface SizeOnly {}

        private int index = 10;
        private String color;
        private Integer length;

        public Cloth(String color, Integer length) {
            this.color = color;
            this.length = length;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }
        @JsonView(SizeOnly.class)
        public Integer getLength() {
            return length;
        }

        public void setLength(Integer length) {
            this.length = length;
        }
    }

}
